package net.kengo.volleyball.scheduler;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class VolleyballSchedulerGUI extends JFrame
{
   private VolleyballSchedulerProperties schedulerProperties       = new VolleyballSchedulerProperties();
   private JTextField                    scheduleLocationTextField = new JTextField();
   private JTextField                    outputDirectoryTextField  = new JTextField();
   private JTextField                    teamNumberTextField       = new JTextField();
   private JButton                       generateButton            = new JButton();

   public VolleyballSchedulerGUI()
   {
      this.initComponents();

      scheduleLocationTextField.setText(schedulerProperties.getScheduleLocation());
      outputDirectoryTextField.setText(schedulerProperties.getOutputDirectory());
      teamNumberTextField.setText(schedulerProperties.getTeamNumber());
   }

   private void initComponents()
   {
      super.setTitle("Volleyball Scheduler");

      JRootPane rootPane = super.getRootPane();
      rootPane.setLayout(new BorderLayout());

      Dimension defaultSize = new Dimension(200, 0);
      scheduleLocationTextField.setPreferredSize(defaultSize);
      outputDirectoryTextField.setPreferredSize(defaultSize);
      teamNumberTextField.setPreferredSize(defaultSize);

      rootPane.add(createMainPanel(), BorderLayout.CENTER);
      rootPane.add(createSouthPanel(), BorderLayout.PAGE_END);

      super.pack();
   }

   private JPanel createMainPanel()
   {
      JPanel panel = new JPanel();

      JLabel scheduleLocationLabel = new JLabel("Location: ");
      JLabel outputDirectoryLabel = new JLabel("Output: ");
      JLabel teamNumberLabel = new JLabel("Team Num: ");

      GroupLayout layout = new GroupLayout(panel);
      panel.setLayout(layout);

      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);

      // Set up the horizontal layout of the components
      layout.setHorizontalGroup(layout
            .createSequentialGroup()
            .addGroup(
                  layout.createParallelGroup().addComponent(scheduleLocationLabel).addComponent(outputDirectoryLabel)
                        .addComponent(teamNumberLabel))
            .addGroup(
                  layout.createParallelGroup().addComponent(scheduleLocationTextField)
                        .addComponent(outputDirectoryTextField).addComponent(teamNumberTextField)));

      // Set up the vertical layout of the components
      layout.setVerticalGroup(layout
            .createSequentialGroup()
            .addGroup(
                  layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(scheduleLocationLabel)
                        .addComponent(scheduleLocationTextField))
            .addGroup(
                  layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(outputDirectoryLabel)
                        .addComponent(outputDirectoryTextField))
            .addGroup(
                  layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(teamNumberLabel)
                        .addComponent(teamNumberTextField)));

      return panel;
   }

   private JPanel createSouthPanel()
   {
      JPanel panel = new JPanel();

      generateButton.setText("Generate");
      generateButton.addActionListener( new GenerateButtonActionListener() );
      panel.add(generateButton);

      return panel;
   }

   
   private class GenerateButtonActionListener implements ActionListener
   {
      @Override
      public void actionPerformed(ActionEvent arg0)
      {
         // Save off current properties
         String scheduleLocation = scheduleLocationTextField.getText();
         String outputDirectory =  outputDirectoryTextField.getText();
         String teamNumber = teamNumberTextField.getText();
         
         schedulerProperties.setScheduleLocation(scheduleLocation);
         schedulerProperties.setOutputDirectory(outputDirectory);
         schedulerProperties.setTeamNumber(teamNumber);
         schedulerProperties.saveProperties();
         
         // Parse the schedule with current properties
         ScheduleParser parser = new ScheduleParser(scheduleLocation, teamNumber);
         parser.parseSchedule();
         
         String scheduleHTML = parser.getHTML();

         // TODO Output the schedule to the outputDirectory
         System.out.println(scheduleHTML);
      }

   }   
   /**
    * @param args
    */
   public static void main(String[] args)
   {
      VolleyballSchedulerGUI gui = new VolleyballSchedulerGUI();
      gui.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      gui.setVisible(true);
   }
}
